package com.runa.rccprotocol.dto.lanjier;

import com.runa.protocol.dto.DtuReportAck;
import com.runa.protocol.util.ByteUtil;
import com.runa.protocol.util.Crc32TJSW;
import com.runa.protocol.util.UtilTime;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author diandian
 * @Date 2022/5/12 20:25
 */
public class LanJiErReportAck extends DtuReportAck {

    //登陆报文  49 4F 54 40 00 16 0E 10 07 06 22 01 02 00 07 20 22 04 28 11 03 51 C1 40 45 43 25 5E
    //响应报文  49 4F 54 40 00 1A 00 10 07 06 22 02 01 00 01 11 02 00 07 20 22 05 09 10 19 00 A4 99 EA 2F 25 5E
    private byte[] registerAckData = {0x49, 0x4F, 0x54, 0x40, 0x00, 0x1A, 0x00,
            0, 0, 0, 0,
            0x02,
            0x01, 0x00, 0x01,
            0x11,
            0x02, 0x00, 0x07,
            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
            0x0, 0x0, 0x0, 0x0,
            0x25, 0x5E};
    private byte[] heartAckData = {0x49, 0x4F, 0x54, 0x40, 0x00, 0x16, 0x04,
            0, 0, 0, 0,
            0x03,
            0x02, 0x00, 0x07,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x0, 0x0, 0x0, 0x0,
            0x25, 0x5E};

    private Crc32TJSW crc32TJSW = new Crc32TJSW();

    public LanJiErReportAck(String netEquNo,Integer type) {
        if (type==1){
            ByteUtil.tenstringToBcd(this.registerAckData, netEquNo, 7, true);
            //序列号
            this.registerAckData[6] = 0x0;

            //时间
            ByteUtil.tenstringToBcd(this.registerAckData, UtilTime.getCurrentTime("yyyyMMddHHmmss"), 19, true);

            byte[] bytesFlip = crc32TJSW.getBytesFlip(this.heartAckData, 4, this.registerAckData.length - 4 - 6);
            //crc32校验
            this.registerAckData[26] = bytesFlip[0];
            this.registerAckData[27] = bytesFlip[1];
            this.registerAckData[28] = bytesFlip[2];
            this.registerAckData[29] = bytesFlip[3];

            this.data=registerAckData;
        }else {
            //序列号
            this.heartAckData[6] = 0x04;
            //时间
            ByteUtil.tenstringToBcd(this.heartAckData, UtilTime.getCurrentTime("yyyyMMddHHmmss"), 15, true);

            byte[] bytesFlip = crc32TJSW.getBytesFlip(this.heartAckData, 4, this.heartAckData.length - 4 - 6);
            //crc32校验
            this.heartAckData[22] = bytesFlip[0];
            this.heartAckData[23] = bytesFlip[1];
            this.heartAckData[24] = bytesFlip[2];
            this.heartAckData[25] = bytesFlip[3];

            this.data=heartAckData;
        }

    }

    public static void main(String[] args) {
        String s = "C02103004022063501000040153836353234323035363430313137393436303034383634353930343837393839383630343636313231394331383332393739240790821F8F0302150C08060092D0";
        byte[] bytes = ByteUtil.hexStringToByte(s);
        byte sum = ByteUtil.sum(bytes, 1, bytes.length - 3);
        byte[] bytes1 = new byte[]{sum};
        System.out.println(ByteUtil.byteToHexString(bytes1));

    }
}